package com.apache.attachment.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.attachment.entity.AttachmentStore;
import com.apache.attachment.manager.AttachmentStoreManager;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;

public class AttachmentStoreManagerImpl implements AttachmentStoreManager {

	protected IDao attachmentStoreDao;
	protected final String entityName = "com.apache.attachment.entity.AttachmentStore";

	public void setAttachmentStoreDao(IDao attachmentStoreDao) {
		this.attachmentStoreDao = attachmentStoreDao;
	}

	public String saveInfo(ParamsVo<AttachmentStore> vo) throws BusinessException {
		AttachmentStore attachmentStore = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="attachmentStore_"+infoId;
		if (null == attachmentStore.getSid() || "".equals(attachmentStore.getSid().trim()))
			attachmentStore.setSid(infoId);
		MethodParam param = new MethodParam("AttachmentStore", cacheKey, "", entityName);
		param.setVaule(attachmentStore);
		if (attachmentStoreDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<AttachmentStore> vo) throws BusinessException {
		AttachmentStore attachmentStore = vo.getObj();
		if ("editObjIdByNow".equals(vo.getMethodKey())) {
			String objId = String.valueOf(vo.getParams("objId"));
			String newObjId = String.valueOf(vo.getParams("newObjId"));
			if (Validator.isNotNull(objId) && Validator.isNotNull(newObjId)) {
				MethodParam param = new MethodParam("AttachmentStoreByObjId", "", "", entityName);
				param.setParams("objId", objId);
				param.setParams("newObjId", newObjId);
				boolean d = attachmentStoreDao.edit(param);
				return d;
			}
		} else if (Validator.isNotNull(attachmentStore.getSid())) {
			String cacheKey = "";
			//String cacheKey="attachmentStore_"+attachmentStore.getSid();
			MethodParam param = new MethodParam("AttachmentStore", cacheKey, "", entityName);
			param.setVaule(attachmentStore);
			return attachmentStoreDao.edit(param);
		}
		return false;
	}

	public boolean editInfoBath(ParamsVo<AttachmentStore> vo) throws BusinessException {
		List<AttachmentStore> asList = vo.getObjs();
		if (!Validator.isEmpty(asList)) {
			String cacheKey = "";
			MethodParam param = new MethodParam("AttachmentStore", cacheKey, "", entityName);
			param.setVaule(asList);
			return attachmentStoreDao.editBath(param);
		}

		return false;
	}

	public boolean deleteInfo(ParamsVo<AttachmentStore> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="attachmentStore_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		AttachmentStore info = (AttachmentStore) attachmentStoreDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("AttachmentStore");
			return attachmentStoreDao.edit(param);
		} else {
			param.setParams("sid", infoId);
			param.setDelete(true);
			return attachmentStoreDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<AttachmentStore> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="attachmentStore_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return attachmentStoreDao.selectById(param);
	}

	public Object execute(ParamsVo<AttachmentStore> vo) {
		String methodKey = vo.getMethodKey();
		if ("editInfoBath".equals(methodKey)) {
			return editInfoBath(vo);
		}
		return null;
	}

	public Page getPageInfo(ParamsVo<AttachmentStore> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = attachmentStoreDao.pageSelect(param);
		return page;
	}

	public List<AttachmentStore> getList(ParamsVo<AttachmentStore> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return attachmentStoreDao.select(param);
	}

	public long countInfo(ParamsVo<AttachmentStore> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return attachmentStoreDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<AttachmentStore> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(attachmentStoreDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		AttachmentStore attachmentStore = vo.getObj();

		if (null == attachmentStore) {
			param.setSqlStr(sb.toString());
			return param;
		}

		if (Validator.isNotNull(attachmentStore.getObjId())) {
			sb.append(" and objId = :objId");
			param.setParams("objId", attachmentStore.getObjId());
		}
		if (Validator.isNotNull(attachmentStore.getObjType())) {
			sb.append(" and objType = :objType");
			param.setParams("objType", attachmentStore.getObjType());
		}
		if (Validator.isNotNull(attachmentStore.getAttachmentName())) {
			sb.append(" and attachmentName = :attachmentName");
			param.setParams("attachmentName", attachmentStore.getAttachmentName());
		}
		if (Validator.isNotNull(attachmentStore.getAttachmentDesc())) {
			sb.append(" and attachmentDesc = :attachmentDesc");
			param.setParams("attachmentDesc", attachmentStore.getAttachmentDesc());
		}
		if (Validator.isNotNull(attachmentStore.getAttachmentType())) {
			sb.append(" and attachmentType = :attachmentType");
			param.setParams("attachmentType", attachmentStore.getAttachmentType());
		}
		if (Validator.isNotNull(attachmentStore.getServerid())) {
			sb.append(" and serverid = :serverid");
			param.setParams("serverid", attachmentStore.getServerid());
		}
		if (Validator.isNotNull(attachmentStore.getDownloadUrl())) {
			sb.append(" and downloadUrl = :downloadUrl");
			param.setParams("downloadUrl", attachmentStore.getDownloadUrl());
		}
		if (Validator.isNotNull(attachmentStore.getCreateUserId())) {
			sb.append(" and createUserId = :createUserId");
			param.setParams("createUserId", attachmentStore.getCreateUserId());
		}
		if (Validator.isNotNull(attachmentStore.getCreateUserRealname())) {
			sb.append(" and createUserRealname = :createUserRealname");
			param.setParams("createUserRealname", attachmentStore.getCreateUserRealname());
		}
		if (Validator.isNotNull(attachmentStore.getCreateSpaceEname())) {
			sb.append(" and createSpaceEname = :createSpaceEname");
			param.setParams("createSpaceEname", attachmentStore.getCreateSpaceEname());
		}
		if (Validator.isNotNull(attachmentStore.getCreateTime())) {
			sb.append(" and createTime = :createTime");
			param.setParams("createTime", attachmentStore.getCreateTime());
		}
		if (Validator.isNotNull(attachmentStore.getAbsolutePath())) {
			sb.append(" and absolutePath = :absolutePath");
			param.setParams("absolutePath", attachmentStore.getAbsolutePath());
		}
		if (Validator.isNotNull(attachmentStore.getMainPicFlag())) {
			sb.append(" and mainPicFlag = :mainPicFlag");
			param.setParams("mainPicFlag", attachmentStore.getMainPicFlag());
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
